<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">所属单位</label>
        <el-select v-model="query.dealer_id" filterable placeholder="请选择" style="width: 185px;" class="filter-item">
          <el-option v-for="item in dict.dealers" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <label class="el-form-item-label">农机制造编号</label>
        <el-input
          v-model="query.eq_code"
          clearable
          placeholder="农机制造编号"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">农机型号</label>
        <el-input
          v-model="query.eq_module"
          clearable
          placeholder="农机型号"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="600px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="所属单位" prop="dealer_id">
            <el-select
              v-model="form.dealer_id"
              filterable
              placeholder="请选择"
              style="width: 370px;"
              @change="changeDealer"
            >
              <el-option v-for="item in dict.dealers" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="设备" prop="eq_id">
            <el-select v-model="form.eq_id" filterable placeholder="请选择" style="width: 370px;">
              <el-option v-for="item in eqs" :key="item.id" :label="item.eq_module" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="作业类型">
            <el-select v-model="form.op_type" filterable placeholder="请选择">
              <el-option v-for="item in dict.operation_type" :key="item.id" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="作业路径">
            <el-input v-model="form.op_path" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="作业幅宽">
            <el-input v-model="form.op_weight" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="作业面积">
            <el-input v-model="form.op_area" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="作业轨迹">
            <el-input v-model="form.op_track" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="作业时间">
            <el-date-picker
              v-model="form.op_time"
              type="datetime"
              style="width: 370px;"
              value-format="yyyy-MM-dd HH:mm:ss"
            />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="dealer_name" label="所属单位" width="200" />
        <el-table-column prop="eq_code" label="农机制造编号" width="100" />
        <el-table-column prop="eq_module" label="农机型号" width="100" />
        <el-table-column prop="op_type" label="作业类型" />
        <el-table-column prop="op_path" label="作业路径" />
        <el-table-column prop="op_weight" label="作业幅宽" />
        <el-table-column prop="op_area" label="作业面积" />
        <el-table-column prop="op_track" label="作业轨迹" />
        <el-table-column prop="op_time" label="作业时间" width="150" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column
          v-if="checkPer(['admin', 'equipmentOperation:edit', 'equipmentOperation:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
/** 农机作业信息 */
import crudEquipmentOperation from '@/api/jf/equipmentOperation'
import { getEqListByDealer } from '@/api/jf/public'
import CRUD, { crud, form, header, presenter } from '@crud/V2/crud'
import crudOperation from '@crud/V2/CRUD.operation'
import pagination from '@crud/V2/Pagination'
import rrOperation from '@crud/V2/RR.operation'
import udOperation from '@crud/V2/UD.operation'

const defaultForm = {
  id: null,
  dealer_id: null,
  eq_id: null,
  op_type: null,
  op_path: null,
  op_weight: null,
  op_area: null,
  op_track: null,
  op_time: null,
  remark: null
}
export default {
  name: 'EquipmentOperation',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '农机作业信息',
      url: 'api/v2/base/equip/equipmentOperation',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudEquipmentOperation }
    })
  },
  dictsV2: ['dealers'],
  dicts: ['operation_type'],
  data() {
    return {
      permission: {
        add: ['admin', 'equipmentOperation:add'],
        edit: ['admin', 'equipmentOperation:edit'],
        del: ['admin', 'equipmentOperation:del']
      },
      rules: {
        dealer_id: [{ required: true, message: '不能为空', trigger: 'blur' }],
        eq_id: [{ required: true, message: '不能为空', trigger: 'blur' }]
      },
      queryTypeOptions: [
        { key: 'dealer_id', display_name: '所属单位' },
        { key: 'eq_code', display_name: '农机制造编号' },
        { key: 'eq_module', display_name: '农机型号' }
      ],
      eqs: []
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.getEqs(form.farm_id, true)
    },
    // 获取设备
    getEqs(dealer_id, isNoClear) {
      if (dealer_id != null) {
        getEqListByDealer(dealer_id)
          .then(res => {
            this.eqs = res.data
          })
          .catch(() => {})
      } else {
        this.eqs = []
      }
      if (!isNoClear) {
        this.form.eq_id = ''
      }
    },
    changeDealer(value) {
      this.getEqs(value)
    }
  }
}
</script>

<style scoped></style>
